//
//  UIView+layout.swift
//  Exchange
//
//  Created by rui on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

extension UIView {
    
    var x: CGFloat {
        get {
            return frame.origin.x
        }
        set {
            var tmpFrame = frame
            tmpFrame.origin.x = newValue
            frame = tmpFrame
        }
    }
    
    var y: CGFloat {
        get {
            return frame.origin.y
        }
        set {
            var tmpFrame = frame
            tmpFrame.origin.y = newValue
            frame = tmpFrame
        }
    }
    
    var width: CGFloat {
        get {
            return frame.size.width
        }
        set {
            var tmpFrame = frame
            tmpFrame.size.width = newValue
            frame = tmpFrame
        }
    }
    
    var height: CGFloat {
        get {
            return frame.size.height
        }
        set {
            var tmpFrame = frame
            tmpFrame.size.height = newValue
            frame = tmpFrame
        }
    }
    
    var right: CGFloat {
        get {
            return x + width
        }
        set {
            x = newValue - width
        }
    }
    
    var bottom: CGFloat {
        get {
            return y + height
        }
        set {
            y = newValue - height
        }
    }
    
    var centerX: CGFloat {
        get {
            return center.x
        }
        set {
            center = CGPoint(x: newValue, y: center.y)
        }
    }
    
    var centerY: CGFloat {
        get {
            return center.y
        }
        set {
            center = CGPoint(x: center.x, y: newValue)
        }
    }
    
    class func loadFromNib() -> UIView? {
        let cls = NSStringFromClass(self)
        guard let c = cls.components(separatedBy: ".").last else {
           return nil
        }
        return Bundle.main.loadNibNamed(c, owner: nil, options: nil)?.last as? UIView
    }
}
